﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.Calibration.CApCalibrationXmlMerger
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System.Collections;
using System.Xml;


namespace DnProcessSimulateCommands.Calibration;

internal class CApCalibrationXmlMerger
{
  private XmlDocument doc_;
  private Hashtable mainRootshash_;
  private Hashtable nodeIdToXmlNodeHash;

  internal XmlDocument Document => this.doc_;

  internal CApCalibrationXmlMerger() => this.Init();

  private void Init()
  {
    this.mainRootshash_ = new Hashtable();
    this.nodeIdToXmlNodeHash = new Hashtable();
    this.doc_ = this.CreateTempCalibrationXmlFile();
    this.BuildMainRootsHash();
  }

  private XmlDocument CreateTempCalibrationXmlFile()
  {
    string calibrationXmlAsString = this.GetDefaultCalibrationXmlAsString();
    XmlDocument calibrationXmlFile = new XmlDocument();
    calibrationXmlFile.LoadXml(calibrationXmlAsString);
    XmlDeclaration xmlDeclaration = calibrationXmlFile.CreateXmlDeclaration("1.0", (string) null, (string) null);
    XmlElement documentElement = calibrationXmlFile.DocumentElement;
    calibrationXmlFile.InsertBefore((XmlNode) xmlDeclaration, (XmlNode) documentElement);
    return calibrationXmlFile;
  }

  private string GetDefaultCalibrationXmlAsString()
  {
    return "<calibration><calibrationSets></calibrationSets></calibration>";
  }

  private void BuildMainRootsHash() => this.BuildMainRootsHash((XmlNode) this.doc_.DocumentElement);

  private void BuildMainRootsHash(XmlNode node)
  {
    this.mainRootshash_.Add((object) node.Name, (object) node);
    for (XmlNode node1 = node.FirstChild; node1 != null; node1 = node1.NextSibling)
      this.BuildMainRootsHash(node1);
  }

  internal void Append(XmlDocument doc) => this.Append((XmlNode) doc.DocumentElement);

  private void Append(XmlNode node)
  {
    for (XmlNode node1 = node.FirstChild; node1 != null; node1 = node1.NextSibling)
    {
      if (this.ShouldAppendChildren(node1))
        this.AppendChildrenOf(node1);
      else
        this.Append(node1);
    }
  }

  private bool ShouldAppendChildren(XmlNode node)
  {
    bool flag = false;
    if (node.Name == "calibrationSets")
      flag = true;
    return flag;
  }

  private void AppendChildrenOf(XmlNode node)
  {
    for (XmlNode node1 = node.FirstChild; node1 != null; node1 = node1.NextSibling)
    {
      if (!this.nodeIdToXmlNodeHash.Contains((object) this.GetXmlNodeHashCode(node1)))
        this.AppendNode(node, node1);
    }
  }

  private void AppendNode(XmlNode parent, XmlNode node)
  {
    XmlNode newParent = this.mainRootshash_[(object) parent.Name] as XmlNode;
    XmlNode node1 = this.CloneNode(parent, node, newParent);
    this.nodeIdToXmlNodeHash.Add((object) this.GetXmlNodeHashCode(node1), (object) node1);
  }

  private XmlNode CloneNode(XmlNode parent, XmlNode node, XmlNode newParent)
  {
    XmlNode newChild = newParent.OwnerDocument.ImportNode(node, true);
    newParent.AppendChild(newChild);
    return newChild;
  }

  private string GetXmlNodeHashCode(XmlNode node)
  {
    string name = node.Name;
    XmlAttributeCollection attributes = node.Attributes;
    if (attributes != null && attributes.Count > 0)
    {
      XmlNode namedItem = attributes.GetNamedItem("ID");
      if (namedItem != null)
        name += namedItem.InnerText;
    }
    return name;
  }
}
